<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\admin\AdminUserModel;
use App\Models\users\UsersModel;
use App\Models\users\UsersWithdrawLogModel;
use App\Service\users\UsersService;
use Carbon\Carbon;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class UsersController extends Controller
{

    /**
     * 获取用户列表页面数据
     */
    public function getUsersListData()
    {
        $data = request()->input();

        $query = UsersModel::select('users.*');

        if ($data['keywords'] != '') {
            $query->where(function ($query) use ($data) {
                $query->where('users.nickname', 'like', '%' . $data['keywords'] . '%')
                    ->orWhere('users.mobile', 'like', '%' . $data['keywords'] . '%');
            });
        }

        if ($data['dateTime'] != '') {
            foreach ($data['dateTime'] as $k => $v) {
                $data['dateTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = strtotime($data['dateTime'][0]);
            $endTime = strtotime($data['dateTime'][1]);
            $query->whereBetween('users.create_time', [$startTime, $endTime]);
        }

        if ($data['uid'] != '') {
            $uid = $data['uid'];
            $query->where('users.id', $uid);
        }

        $lists = $query->orderBy('id', 'desc')->paginate($data['pageSize']);
        foreach ($lists as $v) {
            $v->create_time = Tools::timeConvert($v->create_time, 'Y-m-d');
        }

        return ApiResponse::apiResponseData(200, '获取用户列表数据成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 获取用户详情
     */
    public function getUsersInfoData()
    {
        $uid = request()->input('id');

        $lists = UsersService::instance()->getUserDetail($uid);

        return ApiResponse::apiResponseData(200, '获取用户详情成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 用户表单提交
     */
    public function usersFormSubmit()
    {
        $form = request()->post();

        if (isset($form['id'])) {
            $id = $form['id'];
            $data['is_cancel'] = $form['is_cancel'];
            if ($data['is_cancel'] == 1) {
                $data['cancel_time'] = time();
            }
            unset($form);
            $save = UsersModel::where('id', $id)->update($data);
            if ($save) {
                return ApiResponse::apiResponseData(200, '用户提交成功');
            }
            return ApiResponse::apiResponseData(400, '用户提交失败');
        }
    }


    /**
     * 获取用户提现记录列表页面数据
     */
    public function getUsersWithdrawListData()
    {
        $data = request()->input();

        $query = UsersWithdrawLogModel::leftJoin('users', function ($join) {
            $join->on('users_withdraw_log.uid', '=', 'users.id');
        });

        if ($data['keywords'] != '') {
            $query->where(function ($query) use ($data) {
                $query->where('users.nickname', 'like', '%' . $data['keywords'] . '%')
                    ->orWhere('users.mobile', 'like', '%' . $data['keywords'] . '%');
            });
        }

        if ($data['status'] != '') {
            $query->where('users_withdraw_log.status', $data['status']);
        }

        if ($data['type'] != '') {
            $query->where('users_withdraw_log.type', $data['type']);
        }

        if ($data['dateTime'] != '') {
            foreach ($data['dateTime'] as $k => $v) {
                $data['dateTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = strtotime($data['dateTime'][0]);
            $endTime = strtotime($data['dateTime'][1]);
            $query->whereBetween('users_withdraw_log.create_time', [$startTime, $endTime]);
        }


        $lists = $query->select('users_withdraw_log.*', 'users.nickname', 'users.avatar', 'users.mobile')
            ->orderBy('users_withdraw_log.id', 'desc')
            ->paginate($data['pageSize']);
        if ($lists) {
            foreach ($lists as $v) {
                $v->commission = $v->commission * 100 . '%';
                $v->examine_time = Tools::timeConvert($v->examine_time);
                $v->create_time = Tools::timeConvert($v->create_time);
            }
        }

        return ApiResponse::apiResponseData(200, '获取用户提现记录列表页面数据成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 用户提现记录驳回提交
     */
    public function usersWithdrawRejectSubmit()
    {
        $form = request()->post();

        $username = AdminUserModel::where('id', Request()->system_uid)->value('username');

        $id = $form['id'];
        $data['status'] = 2;
        $data['examine_reason'] = $form['examine_reason'];
        $data['examine_time'] = time();
        $data['examine_admin'] =$username;

        $save = UsersWithdrawLogModel::where('id', $id)->update($data);
        if ($save) {
            return ApiResponse::apiResponseData(200, '用户提现记录驳回提交成功');
        }
        return ApiResponse::apiResponseData(400, '用户提现记录驳回提交失败');
    }


    /**
     * 打款用户提交
     */
    public function usersWithdrawPaySubmit()
    {
        $withdraw_id = request()->post('id');
    }

}
